package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwerT;
import cn.e4j.modular.crm.service.GwerTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 产品成本价格管理
 * Created by shixc on 2019-1-15
 */
@Controller
@RequestMapping("/crm/gwei023")
public class Gwei023Controller extends BaseController {

    @Autowired
    private GwerTService gwerTService;

    /**
     * 跳转产品成本价格管理页面
     */
    @RequiresPermissions("crm:gwei023:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei023.html";
    }

    /**
     * 查询产品区域渠道价格信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwei023:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWERENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwerTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwei023:init")
    @ResponseBody
    public ResponseData add(GwerT gwerT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断编号是否重复
        GwerT gwer = gwerTService.getByKey(shiroUser.getDBC(), gwerT.getGWER001(), gwerT.getGWER004(), gwerT.getGWERPLANT());
        if (gwer != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode(),
                    CrmExceptionEnum.EXISTED_THE_CODE.getMessage());
        }
        gwerTService.add(gwerT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwei023:init")
    @ResponseBody
    public ResponseData edit(GwerT gwerT) {
        gwerTService.edit(gwerT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwei023:init")
    @ResponseBody
    public ResponseData del(GwerT gwerT) {
        gwerTService.delete(gwerT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwei023:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwerT> gwerTList = inDto.getReqList(GwerT.class);
        gwerTService.delDataBatch(gwerTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取产品资料列表
     */
    @ResponseBody
    @RequestMapping("/getGwamList")
    public Dto getGwamList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAMENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwerTService.getGwamList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取币别列表
     */
    @ResponseBody
    @RequestMapping("/getGwacList")
    public Dto getGwacList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWACENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwerTService.getGwacList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取产品单位列表
     */
    @ResponseBody
    @RequestMapping("/getGwabList")
    public Dto getGwabList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWABENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwerTService.getGwabList(inDto);
        outDto.put("list", list);
        return outDto;
    }
}
